package main

import (
	"fmt"
	"reflect"
)

type User struct {
	ID       int    `json:"id" db:"user_id"`
	Username string `json:"username" db:"username"`
	Email    string `json:"email" db:"email_address"`
	private  string // 私有字段
}

func main() {

	user1 := User{
		ID:       1,
		Username: "john_doe",
		Email:    "john@example.com",
		private:  "secret",
	}
	fmt.Println(user1)

	t := reflect.TypeOf(user1)
	v := reflect.ValueOf(user1)

	// panic: reflect: call of reflect.Value.Index on struct Value
	if v.Index(3).CanSet() {
		v.Index(3).SetInt(200)
	}
	fmt.Println("结构体字段数：", t.NumField())
	fmt.Println(v)

	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		value := v.Field(i)

		fmt.Printf("\n字段 %d:\n", i)
		fmt.Printf("  名称: %s\n", field.Name)
		fmt.Printf("  类型: %s\n", field.Type)
		fmt.Printf("  值: %v\n", value.Interface())
		fmt.Printf("  标签: %s\n", field.Tag)

		// 解析标签
		if jsonTag := field.Tag.Get("json"); jsonTag != "" {
			fmt.Printf("  JSON标签: %s\n", jsonTag)
		}
		if dbTag := field.Tag.Get("db"); dbTag != "" {
			fmt.Printf("  DB标签: %s\n", dbTag)
		}
	}
}
